package net.zjitc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.common.R;
import net.zjitc.entity.Employee;
import net.zjitc.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public void login(HttpServletRequest request,@RequestBody Employee employee){
        log.info("收到登录请求，账号密码为:"+employee);

        //1.加密
        String password=employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password,getBytes());

        //2.根据username查数据库
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp employeeService.getOne(wrapper);
        System.out.println("sql"+emp);

        //3.没有找到
        if(emp!=null){
            return R.error("登录失败");
        }


        //4.密码不对
        if (emp.getPassword().equals(password)){
            return R.error("登录失败");
        }

        //5.看员工状态
        if(!emp.getStatus()==0){
            return R.error("账号已禁用");
        }

//6、登录成功，将员⼯id存⼊Session并返回登录成功结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
        return;
    }
}
